<template>
  <el-dialog
    :before-close="close"
    :width="dialogWidth"
    :title="title"
    :visible.sync="editDialog"
    v-if="editDialog"
    center
    v-dialogDrag
  >
    <el-form :model="form" ref="form" :rules="rules" class="mt20">
      <el-col :span="8" class="mb20">
        <el-form-item label="渠道：" prop="promotion" :label-width="formLabelWidth">
          <el-input v-model="form.promotion" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="时间：" prop="time" :label-width="formLabelWidth">
          <el-input v-model="form.time" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="客户电话：" prop="phone" label-width="85px">
          <el-input v-model="form.phone" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="中心：" prop="center" :label-width="formLabelWidth">
          <el-input v-model="form.center" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="部门：" prop="dept" :label-width="formLabelWidth">
          <el-input v-model="form.dept" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="业务员：" prop="salesman" label-width="90px">
          <el-input v-model="form.salesman" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="区域：" prop="distribution" :label-width="formLabelWidth">
          <el-input v-model="form.distribution" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="姓名：" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="定金(元)：" prop="deposit" label-width="90px">
          <el-input
            v-model.number="form.deposit"
            autocomplete="off"
            placeholder="请输入定金(元)"
            disabled
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="订单状态：" prop="ordertaking" label-width="95px">
          <el-switch v-model="form.ordertaking" active-text="已接单" @change="handleOrdertaking"></el-switch>
          <el-switch v-model="isCancell" active-text="已退单" @change="handleCancell"></el-switch>
        </el-form-item>
      </el-col>
      <div v-if="form.ordertaking">
        <el-col :span="8" class="mb20">
          <el-form-item label="类型：" prop="mfrFinance" label-width="95px">
            <el-switch v-model="form.mfrFinance" active-text="厂家金融"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="8" class="mb20">
          <el-form-item label="购买车型：" prop="cartype" label-width="95px">
            <el-input v-model="form.cartype" autocomplete="off" placeholder="请输入车型"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" class="mb20">
          <el-form-item label="提车状态：" prop="pickcar" label-width="95px">
            <el-switch v-model="form.pickcar" active-text="已提车"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="form.pickcar" class="mb20">
          <el-form-item label="提车时间：" prop="pickcartime" label-width="95px">
            <div class="block">
              <el-date-picker
                v-model="form.pickcartime"
                align="right"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions1"
              ></el-date-picker>
            </div>
          </el-form-item>
        </el-col>
      </div>
      <el-col :span="8" v-if="isCancell" class="mb20">
        <el-form-item label="退单原因：" prop="cancellation" label-width="95px">
          <el-select placeholder="请选择退单原因" v-model="form.cancellation">
            <el-option key="无" label="无" value="无"></el-option>
            <el-option v-for="item in cancellationList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer align-center tiling inline-block mt20 mb20">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import "../utils/drag.js";
import { pickerOptions1 } from "@/../config/Stats_options";

export default {
  name: "resources_dialog",
  data() {
    var validateLoanCartype = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入购买车型！"));
      } else if (value.length < 2) {
        return callback(new Error("长度在 2-30 个字符"));
      } else if (value.length > 30) {
        return callback(new Error("长度在 2-30 个字符"));
      } else {
        callback();
      }
    };
    return {
      form: {
        time: "",
        promotion: "",
        deposit: "",
        phone: "",
        name: "",
        center: "",
        dept: "",
        salesman: "",
        distribution: "",
        cancellation: "",
        cartype: "",
        ordertaking: "",
        pickcar: "",
        mfrFinance: "",
        pickcartime: ""
      },
      rules: {
        ordertaking: {
          required: true,
          message: "请选择订单状态！",
          trigger: "blur"
        },
        cartype: { validator: validateLoanCartype, trigger: "blur" },
        cancellation: {
          required: true,
          message: "请选择退单原因！",
          trigger: "blur"
        }
      },
      pickerOptions1,
      isCancell: false,
      formLabelWidth: "70px",
      dialogWidth: "1000px"
    };
  },
  computed: {},
  created() {
    this.getCancellationList();
  },
  watch: {
    editDialog() {
      this.form = {
        time: this.resourcesForm.time,
        promotion: this.resourcesForm.promotionName,
        deposit: this.resourcesForm.deposit,
        phone: this.resourcesForm.phone,
        name: this.resourcesForm.name,
        center: this.resourcesForm.centerName,
        dept: this.resourcesForm.deptName,
        salesman: this.resourcesForm.salesmanName,
        distribution: this.resourcesForm.distributionName,
        cancellation: this.resourcesForm.cancellation,
        cartype: this.resourcesForm.cartype,
        mfrFinance: this.resourcesForm.mfrFinance ? true : false,
        ordertaking: this.resourcesForm.cartype === "" ? false : true,
        pickcar: this.resourcesForm.isPickCar ? true : false,
        pickcartime: this.resourcesForm.pickCarTime
      };
      this.resourcesForm.cancellation === ""
        ? (this.isCancell = false)
        : (this.isCancell = true);
    }
  },
  methods: {
    handleCancell() {
      this.form.cancellation = "无";
    },
    handleOrdertaking() {
      this.form.pickcar = false;
      this.form.pickcartime = this.form.cartype = "";
    },
    getCancellationList() {
      this.$axios.get("/api/system/cancellations").then(data => {
        this.cancellationList = data.map(item => {
          return item.name;
        });
      });
    },
    handleConfirm(formName) {
      // 先进行表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.editResources();
        }
      });
    },
    editResources() {
      this.$axios
        .put("/api/resources/editResources/" + this.resourcesForm.id, this.form)
        .then(data => {
          if (data.status) {
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.$emit("update");
            this.close();
          } else {
            this.$message({
              type: "error",
              message: data.msg
            });
          }
        });
    },
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {
        cancellation: "",
        cartype: "",
        ordertaking: "",
        pickcar: "",
        mfrFinance: "",
        pickcartime: ""
      };
      this.isCancell = false;
    }
  },
  props: ["editDialog", "title", "resourcesForm"]
};
</script>
<style scoped>
</style>
